﻿/* Muaz Khan – http://dashingquill.web.officelive.com */

var settings = window.settings || {};
settings = (function () {
    var dq = dashingquill;
    var sz = Sizzle;

    var handleEvents = function () {
        dq.bind('#save-changes', 'click', function (saveChangesButton) {
            dq.hide('#on-saving-changes');
            saveChangesButton.innerHTML = 'Saving Changes...';

            var fields = sz('#fullname, #website, #about-you');
            if (!validate(fields[0])) {     /* fullName is required! */
                saveChangesButton.innerHTML = 'Save Changes &gt;&gt;';
                return;
            }

            var data = '?fullName=' + fields[0].value + '&website=' + fields[1].value + '&about=' + dq.validate(fields[2].value);
            dq.xhr({
                url: dq.domainName + '/SaveAccountSettings' + data,
                callback: function (response) {
                    if (response.IsSuccess == true) {
                        saveChangesButton.innerHTML = 'Save Changes';
                        dq.show('#on-saving-changes', 'inline');
                    }
                    else {
                        
                        var onSavingChanges = dq.find('#on-saving-changes');
                        if (onSavingChanges) {
                            onSavingChanges.innerHTML = '--- Something went wrong. Refresh the page ---';
                            dq.show(onSavingChanges);
                        }
                        alert(response.Error);
                    }
                }
            });
        });
        
        var you = dq.find('#you');
        if (you) {
            dq.bind(you, 'click', function () {
                location = dq.domainName + '/' + you.getAttribute('accesskey');
            });
        }

        dq.bind('#delete-account-request', 'click', function () {
            var deleteAccountVerificationBox = dq.find('#verify-delete-account-request');
            if (deleteAccountVerificationBox) {
                deleteAccountVerificationBox.style.maxHeight = '600px';
                deleteAccountVerificationBox.style.opacity = '1';
            }
        });        

        dq.bind('#never', 'click', function () {
            var deleteAccountVerificationBox = dq.find('#verify-delete-account-request');
            if (deleteAccountVerificationBox) {                
                deleteAccountVerificationBox.style.maxHeight = '0';
                dq.once(6, function () {
                    deleteAccountVerificationBox.style.opacity = '0';
                });
            }
        });

        dq.bind('#delete-account-permanently', 'click', function (deleteAccontPermanentlyButton) {
            dq.hide(sz('#never, #delete-account-request'));
            deleteAccontPermanentlyButton.onclick = null;
            var deleteAccountVerificationBox = dq.find('#verify-delete-account-request');
            if (deleteAccountVerificationBox) {
                deleteAccountVerificationBox.className += ' non-stop-deleting';
                deleteAccountVerificationBox.innerHTML = 'Deleting ---- Now it will never stop!';                
            }            

            dq.xhr({
                url: dq.domainName + '/DeleteAccountPermanently?securityToken=' + dq.find('#securityToken').value,
                callback: function (response) {
                    if (response.IsSuccess == true) {
                        deleteAccountVerificationBox.innerHTML = 'Your account deleted successfully - going to homepage...';
                        location = dq.domainName + '/';
                    }
                }
            });
        });
    };

    var validate = function (field) {
        if (field.value.length <= 0) {
            field.style.borderColor = 'red';
            return false;
        }
        else field.style.borderColor = '#DCDCDC';
        return true;
    };

    return {
        handleEvents: function () {
            return handleEvents();
        }
    };
})(settings);